<?php
namespace Kxdr\Model;
use Think\Model;
class GiftsModel extends Model {
	public	$picurl;
	public	$giftname;
	public	$gift_type;
	public	$lott;
	public	$jf;
	public	$promoid;
	public	$tagid;
	public	$medal_id;
	public	$medal_name;
	public	$medal_pic;
	public	$gifturl;
	public	$price;
	public	$quantity;
	public	$kmlist;
	public	$detail;
	public	$sid;
	public	$giftid;
	private $km_table = "GiftsKm";
	private function prepare_data(){
		$data["level"]=0;
		$data["probability"]=0;
		$data["num_iids"]="";
		$data["event_id"]=0;
		$data["sid"]=$this->sid;
		$data["details"]=$this->detail;
		$data["name"]=$this->giftname;
		$data["type"]=$this->gift_type;
		$data["price"]=$this->price;
		$data["quantity"]=$this->quantity;
		$data["quantity_initial"]=$this->quantity;
		$data["pic_url"]=$this->picurl;
		$data["link"]=$this->gifturl;
		if($this->promoid){
		$data["promoid"]=$this->promoid;
		$data["tagid"]=$this->tagid;
		}
		switch($this->gift_type){
			case 7 : 
			//抽奖次数保存在cfg内
			// $cfg["cfg"]=Array("zailai"=>$lott); //不使用json
			$data["zailai"]=$this->lott;
			break;
			case 10:
			//奖励积分保存在cfg内：
			// $cfg["cfg"]=Array("jifen"=>$jf);//不使用json
			$data["jifen"]=$this->jf;
			break;
			case 11:
			//荣誉：
			// $cfg["cfg"]=Array("rongyu"=>Array("medal_id"=>$medal_id,"name"=>$medal_name,"pic_url"=>$medal_pic));
			$data["medal_id"]=$this->medal_id;
			$data["medal_name"]=$this->medal_name;
			$data["medal_pic"]=$this->medal_pic;
			break;
		}
		return $data;
	}
	public function save_gift(){
			$data = $this->prepare_data();
			unset($data["type"]); //本版不支持更改奖品类型
			$re = $this->where("`id`=".$this->giftid." AND `sid`=".$this->sid)->save($data);
			if($re){
				switch($this->gift_type)
				{
					case 4 :
						$KM = M($this->km_table);
						
						// 将老的卡密作废;或许可以直接删除
						$KM->where("`giftid`=".$this->giftid." AND `sid`=".$this->sid ." AND `status`=0")->save(Array("status"=>8));
						// 加入新的卡密
						$vars = explode("\n",$this->kmlist);
						$ts = time().rand(100,999);
						foreach($vars as $var){
							$tmp = explode(",",$var);
							$da=array();
							if($tmp[1]){
								$da["tscode"]=$ts;
								$da["cardno"]=$tmp[0];
								$da["secret"]=$tmp[1];
								$da["giftid"]=$this->giftid;
								$da["sid"]=$this->sid;
								$kmdata[]=$da;
							}
						}
						$kmre = $KM->addAll($kmdata);
						$data["quantity"]=count($kmdata);
						$this->where("`id`=".$this->giftid)->save($data);
					break;
				}
				return true;
			}else{
				return false;
			}
	}
	public function add_gift(){
			$data = $this->prepare_data();
			// $data["cfg"]=json_encode($cfg);
			// $GIFT = M("KxdrGifts");
			$re = $this->add($data);
			if($re){
				$reda["hasError"]=false;
				switch($this->gift_type){
					case 4 : //处理卡密
						$vars = explode("\n",$this->kmlist);
						$ts = time().rand(100,999);
						foreach($vars as $var){
							$tmp = explode(",",$var);
							$da=array();
							if($tmp[1]){
								$da["tscode"]=$ts;
								$da["cardno"]=$tmp[0];
								$da["secret"]=$tmp[1];
								$da["giftid"]=$re;
								$da["sid"]=$this->sid;
								$kmdata[]=$da;
							}
						}
						$KM = M($this->km_table);
						$kmre = $KM->addAll($kmdata);
						if($kmre){
							$kmct = count($kmdata);
							$this->save(Array("id"=>$re,"quantity"=>$kmct,"quantity_initial"=>$kmct));
							$status["msg"]="成功保存卡密！已保存".count($kmdata)."个卡密";
							$status["tscode"]=$ts;
						}else{
							$reda["hasError"]=true;
							$reda["kmerror"]=$KM->getDbError();
							if(count($kmdata)==0){
								$msg = "可保存的卡密为0个，请确认卡密的格式。";
							}
							$reda["error"]="保存卡密失败。。。".$msg;
						}
					break;
					case 5 : //处理折扣
					
					break;
					case 11 : //处理荣誉
					break;
				}
			}else{
				$reda["hasError"]=true;
				$reda["error"]=$this->getDbError();
			}
		return $reda;
	}
}